package com.angel.components.impresion
{

	import com.angel.components.imprint.Printer;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	import mx.printing.FlexPrintJob;
	
	public class DataGridPrinter
	{
		[Bindable]
		public var columns:Array;
		[Bindable]
		public var objects:ArrayCollection;
		[Bindable]		
		public var tittle:String;
		[Bindable]
		public var description:String;
	
		public function DataGridPrinter(__data:ArrayCollection, __columns:Array, __tittle:String, __description:String){
			columns = __columns;
			tittle = __tittle;
			objects = __data;
			description = __description;
		}
		
		/**
		 * Imprime por impresora un listado de los objetos que se pasaron en la creacion, mostrando las columnas que
		 * se definiero en el momento de creacion.
		 * 
		 */
		public function print(formPrintView:FormPrintView):void{
			var printJob:FlexPrintJob = new FlexPrintJob();
            if (printJob.start()) {
                // Create a FormPrintView control as a child of the current view.
                var thePrintView:FormPrintView = formPrintView;
                Application.application.setStyle(" background-color","#FFFFFF");
               Application.application.addChild(thePrintView);

                //Seteo las propiedades de la vista de impresion.
                thePrintView.width = printJob.pageWidth;
                thePrintView.height = printJob.pageHeight;
                thePrintView.tittle = tittle;
                thePrintView.columns = columns;
                thePrintView.description = description;
                thePrintView.objectsTotal = ((objects != null)? objects.length : 0);
                
                // Set the data provider of the FormPrintView component's data grid to be the data provider of the displayed data grid.
                if(thePrintView.myDataGrid.columns.length > 0){
                	thePrintView.myDataGrid.dataProvider = objects;
                } else {
                	thePrintView.myAdvancedDataGrid.dataProvider = objects;
                }
                
                // Create a single-page image.
                thePrintView.showPage(FormPrintView.PAGE_TYPE_MIDDLE);
                
                if(thePrintView.myDataGrid.columns.length > 0){
                	this.processPrintWithDataGrid(thePrintView, printJob);
                } else {
                	this.processPrintWithAdvancedDataGrid(thePrintView, printJob);
                }
                
                // All pages are queued; remove the FormPrintView control to free memory.
                Application.application.removeChild(thePrintView);
            }
            // Send the job to the printer.
            printJob.send();
		}
		
		private function processPrintWithDataGrid(thePrintView:FormPrintView, printJob:FlexPrintJob):void{
			// If the print image's data grid can hold all the provider's rows, add the page to the print job.
            if(!thePrintView.myDataGrid.validNextPage) {
                printJob.addObject(thePrintView);
            } else {
            	// Otherwise, the job requires multiple pages.
                // Create the first page and add it to the print job.
                thePrintView.showPage(FormPrintView.PAGE_TYPE_FIRST);
                printJob.addObject(thePrintView);
                thePrintView.pageNumber++;
                // Loop through the following code until all pages are queued.
                while(true)
                {
                    // Move the next page of data to the top of the print grid.
                    thePrintView.myDataGrid.nextPage();
                    thePrintView.showPage(FormPrintView.PAGE_TYPE_LAST);
                    /* 
                    	If the page holds the remaining data, or if the last page was completely filled by the last grid data, queue it for printing.
                     	Test if there is data for another PrintDataGrid page.
                     */
                    if(!thePrintView.myDataGrid.validNextPage)
                    {
                        // This is the last page; queue it and exit the print loop.
                        printJob.addObject(thePrintView);
                        break;
                    } else {
                    	// This is not the last page. Queue a middle page.
                        thePrintView.showPage(FormPrintView.PAGE_TYPE_MIDDLE);
                        printJob.addObject(thePrintView);
                        thePrintView.pageNumber++;
                    }
                }
            }
		}
		
		private function processPrintWithAdvancedDataGrid(thePrintView:FormPrintView, printJob:FlexPrintJob):void{
			// If the print image's data grid can hold all the provider's rows, add the page to the print job.
            if(!thePrintView.myAdvancedDataGrid.validNextPage) {
                printJob.addObject(thePrintView);
            } else {
            	// Otherwise, the job requires multiple pages.
                // Create the first page and add it to the print job.
                thePrintView.showPage(FormPrintView.PAGE_TYPE_FIRST);
                printJob.addObject(thePrintView);
                thePrintView.pageNumber++;
                // Loop through the following code until all pages are queued.
                while(true)
                {
                    // Move the next page of data to the top of the print grid.
                    thePrintView.myAdvancedDataGrid.nextPage();
                    thePrintView.showPage(FormPrintView.PAGE_TYPE_LAST);
                    /* If the page holds the remaining data, or if the last page was completely filled by the last grid data, queue it for printing.
                     	Test if there is data for another PrintDataGrid page. */
                    if(!thePrintView.myAdvancedDataGrid.validNextPage){
                        // This is the last page; queue it and exit the print loop.
                        printJob.addObject(thePrintView);
                        break;
                    } else {
                    	// This is not the last page. Queue a middle page.
                        thePrintView.showPage(FormPrintView.PAGE_TYPE_MIDDLE);
                        printJob.addObject(thePrintView);
                        thePrintView.pageNumber++;
                    }
                }
            }
		}
	}	
}